package com.bishe.controller;

import cn.hutool.core.util.StrUtil;
import com.bishe.pojo.*;
import com.bishe.serivce.IApplyService;
import com.bishe.serivce.IUserService;
import com.bishe.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 申请控制层
 * @date 2022/03/20 41:08:41
 */
@RestController
@RequestMapping("/apply")
@Api(tags = "个人申请控制器")
@CrossOrigin
public class ApplyController {
    @Autowired
    private IApplyService applyService;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private IUserService userService;

    @ApiOperation("添加和修改个人申请")
    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody Apply apply, HttpServletRequest httpServletRequest){
        JobUtil.checkUserLogin(httpServletRequest,jdbcTemplate);
        if (apply == null){
            throw new EcoBootException("apply 为空！");
        }
        applyService.addOrUpdate(apply,httpServletRequest);

        return Result.ok();
    }

    @ApiOperation("删除个人申请")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id")String id){


        applyService.deleteById(id);

        return Result.ok();
    }

    @ApiOperation("个人申请分页 标题模糊")
    @GetMapping("/getPage")
    public Result getPage(QueryModel queryModel,HttpServletRequest request){
        //检查登录
        String userType = JobUtil.getUserType(request);
        if (StrUtil.isEmpty(userType)||!"经理".equals(userType)){
            throw new NotLoginException("未登录！");
        }

        List<Apply> data = null;
        Integer pageNo = queryModel.getPageNo();
        Integer pageSize = queryModel.getPageSize();
        String searchTxt = queryModel.getSearchTxt();
        String createBy = queryModel.getCreateBy();
        SqlQueryUtils<Apply> applySqlQueryUtils = new SqlQueryUtils<>(Apply.class, jdbcTemplate);
        //默认 一页10个数据
        if (pageNo == null){
            pageNo = 1;
        }
        if (pageSize == null){
            pageSize = 10;
        }
        //搜索标题
        if (StrUtil.isNotEmpty(searchTxt)){
            applySqlQueryUtils.like(Apply::getTitle,searchTxt);
        }
        //搜索人姓名
        if (StrUtil.isNotEmpty(createBy)){
            List<User> users = userService.likeName(createBy); //模糊搜索
            applySqlQueryUtils.in(Apply::getCreateBy,users);
        }
        //创建时间倒叙
        applySqlQueryUtils.orderByDesc(Apply::getCreateTime);
        //都是0 不分页
        if (pageNo == 0 && pageSize == 0){
            data  =  applySqlQueryUtils.getList();
        }else {
            data =  applySqlQueryUtils.limit(pageNo,pageSize).getList(); //分页
        }
        //将人id翻译成name
        for (Apply s : data) {
            String createBy1 = s.getCreateBy();
            User one = new SqlQueryUtils<User>(User.class, jdbcTemplate).eq(User::getId, createBy1).getOne();
            if(one!=null){
                s.setCreateByName(one.getNikName());
            }
        }
       return Result.ok(new PageResult<Apply>(queryModel,applySqlQueryUtils,data));
    }


    @ApiOperation("获取自己申请列表")
    @PostMapping("/getApply")
    public Result getApply(QueryModel queryModel,HttpServletRequest request){
        //检查登录
        String userType = JobUtil.getUserType(request);
        if (StrUtil.isEmpty(userType)||!"员工".equals(userType)){
            throw new NotLoginException("未登录！");
        }

        List<Apply> data = null;
        Integer pageNo = queryModel.getPageNo();
        Integer pageSize = queryModel.getPageSize();
        String searchTxt = queryModel.getSearchTxt();
        SqlQueryUtils<Apply> applySqlQueryUtils = new SqlQueryUtils<>(Apply.class, jdbcTemplate);

        if (pageNo == null){
            pageNo = 1;
        }
        if (pageSize == null){
            pageSize = 10;
        }
        if (StrUtil.isNotEmpty(searchTxt)){
            applySqlQueryUtils.like(Apply::getTitle,searchTxt);
        }
        applySqlQueryUtils.eq(Apply::getCreateBy,JobUtil.getUserId(request));
        applySqlQueryUtils.orderByDesc(Apply::getCreateTime);
        //都是0 不分页
        if (pageNo == 0 && pageSize == 0){
            data  = applySqlQueryUtils.getList();
        }else {
            data = applySqlQueryUtils.limit(pageNo,pageSize).getList();
        }
        for (Apply s : data) {
            String createBy1 = s.getCreateBy();
            User one = new SqlQueryUtils<User>(User.class, jdbcTemplate).eq(User::getId, createBy1).getOne();
            if(one!=null){
                s.setCreateByName(one.getNikName());
            }
        }

        return Result.ok(new PageResult<Apply>(queryModel,applySqlQueryUtils,data));
    }
   
}
